import { UserConfigExport, ConfigEnv } from 'vite'
import { createVitePlugins } from './build/vite/plugin'

import pkg from './package.json'

const { name, version } = pkg
const { resolve } = require('path')

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir)
}

// https://vitejs.dev/config/
export default ( { command }: ConfigEnv): UserConfigExport => {
  return {
    plugins: createVitePlugins(),
    resolve: {
      // src路径别名
      alias: [
        {
          find: '@',
          replacement: pathResolve('src') + '/'
        }
      ],
      extensions: ['.ts', '.js', '.tsx', '.json']
    },
    server: {
      open: '/',
      proxy: {
        '/api': {
          target: 'http://localhost:8080',
          changeOrigin: true,
          rewrite: path => path.replace('/api', '/dev-api')
        },
        '/mock': {
          target: 'http://localhost:3000',
          changeOrigin: true,
          rewrite: path => path.replace('/api', '/mock')
        }
      }
    }
  }
}
